//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package weaver.interfaces.expense.action.gzc;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.util.ExpenseCompare;
import weaver.interfaces.util.ExpenseCompareUtil;
import weaver.interfaces.util.ExpenseInfoUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 
 * @ClassName: KYSJCGSQ_ExpenseAction 
 * @Description: 科研试剂采购申请预算检查
 * @author xiyufei
 * @date 2017-9-12 上午10:41:34 
 *
 */
public class KYSJCGSQ_ExpenseAction extends BaseBean implements Action {
 
    public String execute(RequestInfo requestInfo) {
        try {
            String workflowid = requestInfo.getWorkflowid();
            String requestid = requestInfo.getRequestid();
            this.writeLog("进入KYSJCGSQ_ExpenseAction-----------" + requestid);
            RecordSet rs = new RecordSet();
            rs.executeSql("select formid from workflow_base where id = " + workflowid);
            rs.next();
            String formid = rs.getString("formid");
            String formtable = "formtable_main_" + formid.replaceAll("-", "");
            String iskt = "0";
            String ccode = "";
            double amount = 0.0D;
            String cexpenseitemcode = "";
            String citemcode = "";
            String ly = "";
            String iyear = "";
            rs.execute("select id,ks,zcxm,zje,zjly,rq,kth1,huansuan from " + formtable + " where requestid=" + requestid);
            rs.next();
            ExpenseInfoUtil expenseInfoUtil = new ExpenseInfoUtil();
            ccode = expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks")));
            amount = Util.getDoubleValue(rs.getString("huansuan"), 0.0D);
            cexpenseitemcode = Util.null2String(rs.getString("zcxm"));
            String kth = Util.null2String(rs.getString("kth1"));
            ly = Util.null2String(rs.getString("zjly"));
            iyear = Util.null2String(rs.getString("rq")).substring(0, 4);
            ExpenseCompareUtil expenseCompareUtil = new ExpenseCompare();
            //扣除预算前先计算汇率
//            amount = expenseInfoUtil.huilvjisuanYqsb(requestid,formtable);
            
            if("0".equals(ly) || "1".equals(ly)|| "2".equals(ly)) {//资金来源为课题或者临床试验经费、财政拨款时 课题号+项目支出
                iskt = "1";
                if(!"".equals(cexpenseitemcode)) {
                    if(!expenseCompareUtil.compareExpense2(workflowid, cexpenseitemcode, kth, amount, iyear)) {
                        requestInfo.getRequestManager().setMessageid("流程提交失败");
                        requestInfo.getRequestManager().setMessagecontent("课题号:" + kth + " ,支出项目:" + expenseInfoUtil.getCItemName(cexpenseitemcode) + "的预算不足");
                        return "1";
                    }
                } else if(!expenseCompareUtil.compareExpense3(workflowid,cexpenseitemcode,kth, amount, iyear)) {
                    requestInfo.getRequestManager().setMessageid("流程提交失败");
                    requestInfo.getRequestManager().setMessagecontent("课题号:" + kth + "的预算不足");
                    return "1";
                }
            }

            if("3".equals(ly) && !expenseCompareUtil.compareExpense1(workflowid, ccode, cexpenseitemcode, citemcode, amount, iyear)) {//医院经费：科室+支出项目
                requestInfo.getRequestManager().setMessageid("流程提交失败");
                requestInfo.getRequestManager().setMessagecontent("科室:" + expenseInfoUtil.getCname(ccode) + ",支出项目:" + expenseInfoUtil.getCItemName(cexpenseitemcode) + ",项目:" + expenseInfoUtil.getCtn(citemcode) + "的预算不足");
                return "1";
            }  
 
              if(!"5".equals(ly) && !expenseInfoUtil.subtractExpense(workflowid, ccode, cexpenseitemcode, citemcode, kth, amount, iyear, iskt)) {
                    //requestInfo.getRequestManager().setMessageid("流程保存失败");
            	  	requestInfo.getRequestManager().setMessageid("流程提交失败");
                    requestInfo.getRequestManager().setMessagecontent("预算更新失败");
                    return "1";
                }
            this.writeLog("结束：KYSJCGSQ_ExpenseAction-----------" + requestid);
//              requestInfo.getRequestManager().setMessageid("流程提交失败");
//              requestInfo.getRequestManager().setMessagecontent("成功");
              return "1";
        } catch (Exception var18) {
        	//requestInfo.getRequestManager().setMessageid("流程提交失败");
        	//requestInfo.getRequestManager().setMessagecontent(var18.getMessage());
            return "1";
        }
    }
}
